package cn.iocoder.yudao.module.erp.service.business.money.moneyincome;

import cn.iocoder.yudao.module.erp.controller.admin.business.money.moneyincome.vo.MoneyIncomePageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.money.moneyincome.vo.MoneyIncomeSaveReqVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.module.erp.dal.dataobject.business.money.moneyincome.MoneyIncomeDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.erp.dal.mysql.business.money.moneyincome.MoneyIncomeMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.*;

/**
 * 收入类型 Service 实现类
 *
 * @author yy
 */
@Service
@Validated
public class MoneyIncomeServiceImpl implements MoneyIncomeService {

    @Resource
    private MoneyIncomeMapper moneyIncomeMapper;

    @Override
    public Long createMoneyIncome(MoneyIncomeSaveReqVO createReqVO) {
        // 插入
        MoneyIncomeDO moneyIncome = BeanUtils.toBean(createReqVO, MoneyIncomeDO.class);
        moneyIncomeMapper.insert(moneyIncome);
        // 返回
        return moneyIncome.getId();
    }

    @Override
    public void updateMoneyIncome(MoneyIncomeSaveReqVO updateReqVO) {
        // 校验存在
        validateMoneyIncomeExists(updateReqVO.getId());
        // 更新
        MoneyIncomeDO updateObj = BeanUtils.toBean(updateReqVO, MoneyIncomeDO.class);
        moneyIncomeMapper.updateById(updateObj);
    }

    @Override
    public void deleteMoneyIncome(Long id) {
        // 校验存在
        validateMoneyIncomeExists(id);
        // 删除
        moneyIncomeMapper.deleteById(id);
    }

    private void validateMoneyIncomeExists(Long id) {
        if (moneyIncomeMapper.selectById(id) == null) {
            throw exception(MONEY_INCOME_NOT_EXISTS);
        }
    }

    @Override
    public MoneyIncomeDO getMoneyIncome(Long id) {
        return moneyIncomeMapper.selectById(id);
    }

    @Override
    public PageResult<MoneyIncomeDO> getMoneyIncomePage(MoneyIncomePageReqVO pageReqVO) {
        return moneyIncomeMapper.selectPage(pageReqVO);
    }

}